package top.infopub.mgr.adm.web;


import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import top.infopub.mgr.adm.domain.BorrowRecordAssist;
import top.infopub.mgr.adm.domain.BorrowRecordDomain;
import top.infopub.mgr.adm.service.BorrowRecordService;
import top.infopub.mgr.adm.vo.BorrowRecordVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.mgr.util.LogUtil;
import top.infopub.mgr.util.LogUtil.AcctionType;
import top.infopub.mgr.util.LogUtil.LogModelType;
import top.infopub.model.adm.BorrowRecordList;
import top.infopub.model.hr.StaffInfo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;
import top.infopub.util.IdFactory;

import com.github.pagehelper.Page;


/**
 * 档案借阅申请
 * @author Awoke
 * @version 2020年3月23日
 * @see BorrowRecordController
 * @since
 */
@Controller
@RequestMapping("/borrowRecord")
public class BorrowRecordController extends GenericFlowController {

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    @Autowired
    private StaffInfoService staffInfoService;

    @Autowired
    private BorrowRecordService borrowRecordService;

    /**
     * list页面
     * @return
     */
    @RequestMapping("/list")
    public String list() {
        return "adm/lay-borrowRecord-list";
    }

    /**
     * 档案借阅申请列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<BorrowRecordDomain> queryDataByCondition(BorrowRecordVo borrowRecordVo,
                                                          ParamPage paramPage) {
        Page<BorrowRecordDomain> page = new Page<BorrowRecordDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (borrowRecordVo.getEndDate() != null) {
            borrowRecordVo.setEndDate(DateUtils.addDays(borrowRecordVo.getEndDate(), 1));
        }
        page = borrowRecordService.selectByDomainWhere(page, borrowRecordVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 档案借阅申请-我审批的列表数据
     *
     * @return
     */
    @RequestMapping("/queryMyCheckDataByCondition")
    @ResponseBody
    public RspVo<BorrowRecordDomain> queryMyCheckDataByCondition(BorrowRecordVo borrowRecordVo,
                                                                 ParamPage paramPage) {
        Page<BorrowRecordDomain> page = new Page<BorrowRecordDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (borrowRecordVo.getEndDate() != null) {
            borrowRecordVo.setEndDate(DateUtils.addDays(borrowRecordVo.getEndDate(), 1));
        }
        borrowRecordVo.setAddBy(SessionUserUtils.getSessionUserId());
        page = borrowRecordService.selectMyCheckByDomainWhere(page, borrowRecordVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 跳转到新增页面
     * @return
     */
    @RequestMapping("/add")
    public String toAdd(HttpServletRequest request) {
        String bindOrgId = String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgId"));
        String bindOrgName = String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgName"));
        String bindDeptId = String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptId"));
        String bindDeptName = String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptName"));
        request.setAttribute("bindOrgId", bindOrgId);
        request.setAttribute("bindOrgName", bindOrgName);
        request.setAttribute("bindDeptId", bindDeptId);
        request.setAttribute("bindDeptName", bindDeptName);
        AuthorizationVO authorizationVO = SessionUserUtils.getSessionAttributeForUserDtl();
        StaffInfo staffInfo = staffInfoService.selectByPrimaryKey(authorizationVO.getHrStaffId());
        request.setAttribute("staffName", staffInfo.getStaffName());
        return "adm/lay-borrowRecord-add";
    }

    /**
     * 档案借阅申请-新增
     * @throws ParseException 
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(BorrowRecordAssist borrowRecordAssist)
        throws ParseException {

        //档案借阅申请表处理
        borrowRecordAssist.setHrDeptId(borrowRecordAssist.getHrDeptid());
        borrowRecordAssist.setHrOrgId(borrowRecordAssist.getHrOrgid());
        borrowRecordAssist.setStaffName(borrowRecordAssist.getStaffname());
        borrowRecordAssist.setHrStaffApplyId(borrowRecordAssist.getHrStaffApplyid());
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        String id = IdFactory.getUUIDUpper();
        borrowRecordAssist.setId(id);
        borrowRecordAssist.setAddBy(vo.getId());
        borrowRecordAssist.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
        String code = codeGeneratorConfigService.generatorCode("adm_borrow_record");
        borrowRecordAssist.setCodes(code);
        // 处理processId
        borrowRecordAssist.setProcessId(IdFactory.getUUIDUpper());
        borrowRecordAssist.setProcessStatus("1");//审批中
        borrowRecordAssist.setUptBy(vo.getId());
        borrowRecordAssist.setUptTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());

        //清单
        List<BorrowRecordList> borrowRecordLists = new ArrayList<BorrowRecordList>();
        String[] showIdxList = borrowRecordAssist.getShowIdxList();
        if (showIdxList != null && showIdxList.length > 0) {
            for (int i = 0; i < showIdxList.length; i++ ) {
                String showIdx = showIdxList[i];
                String docType = borrowRecordAssist.getDocTypeList()[i];
                String docName = borrowRecordAssist.getDocNameList()[i];
                String borrowRecordListId = IdFactory.getUUIDUpper();
                BorrowRecordList borrowRecordList = new BorrowRecordList();
                borrowRecordList.setId(borrowRecordListId);
                borrowRecordList.setRecordId(id);
                borrowRecordList.setShowIdx(Integer.parseInt(showIdx));
                borrowRecordList.setDocType(docType);
                borrowRecordList.setDocName(docName);
                borrowRecordList.setAddBy(vo.getId());
                borrowRecordList.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
                borrowRecordLists.add(borrowRecordList);
            }
        }
        borrowRecordService.saveBorrowRecord(borrowRecordAssist, borrowRecordLists);
        LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD, null, null);
        return RspVo.getSuccessResponse();
    }

    /**
     * 根据id查询设备合同详细  > 注意：方法命名必须为：toDealOrDetail
     * @param id 档案借阅申请id
     * @return
     * @see
     */
    @RequestMapping("/toDealOrDetail")
    public ModelAndView queryById(HttpServletRequest request, String id, String processDealWith) {
        Map<String, Object> map = new HashMap<String, Object>();
        BorrowRecordVo borrowRecordVo = borrowRecordService.queryById(id);
        request.setAttribute("modelDetail", borrowRecordVo);
        map.put("borrowRecordVo", borrowRecordVo);
        // 审批flag
        request.setAttribute("processDealWith", processDealWith);
        // 审批data
        processData(request, borrowRecordVo.getProcessId());
        return new ModelAndView("adm/lay-borrowRecord-detail", map);
    }
}